import argparse
import logging
import unittest

import numpy as np

from chapter03_K_Nearest_Neighbors.KNN import KNN

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

class TestStringMethods(unittest.TestCase):

    def test_e31(self):
        X = np.loadtxt("./data/data_1.txt")
        # print(X-X[0])
        # axis为1计算每一行的1范数
        # axis为0计算每一列的1范数
        rst = np.linalg.norm(X - X[0], ord=1, axis=1)
        for p in range(2, 5):

            rst = np.vstack((rst, np.linalg.norm(X-X[0], ord=p, axis=1)))
        # Lp(x1,x2)
        self.assertListEqual(np.round(rst[:, 1], 2).tolist(), [4]*4)
        # Lp(x1,x3)
        self.assertListEqual(np.round(rst[:, 2], 2).tolist(), [6, 4.24, 3.78, 3.57])
        # print(np.round(rst[:, 2], 2).tolist())

    def test_e32(self):
        X = np.loadtxt("./data/data_2.txt")
        clf = KNN()
        clf.fit(X)
        logger.info(clf.kdtree)

    def test_q32(self):
        X = np.loadtxt("./data/data_2.txt")
        target = np.array([3, 4.5])
        clf = KNN()
        clf.fit(X)
        rst = clf.predict(target)
        self.assertListEqual([4, 7], rst.tolist())
        logger.warning(rst)

if __name__ == '__main__':

    ap = argparse.ArgumentParser()
    ap.add_argument("-p", "--path", required=False, help="path to input data file")
    args = vars(ap.parse_args())
    unittest.main()